package com.weilus.controller.permission;

import com.weilus.config.security.SecurityMetadataSourcePropertity;
import com.weilus.controller.AdminController;
import com.weilus.dto.RespEntity;
import com.weilus.dto.request.PUserDto;
import com.weilus.entity.SecurityGroup;
import com.weilus.service.permission.RoleService;
import com.weilus.service.permission.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController extends AdminController{

	@Autowired
	UserService userService;
	@Autowired
	RoleService roleService;
	@Autowired
	SecurityMetadataSourcePropertity adminSecurityPropertity;

	@GetMapping("/user.html")
	public String queryUser(){
		return "permission/user_table";
	}

	@PostMapping("/user")
	public @ResponseBody RespEntity add(PUserDto user){
		return RespEntity.ok(userService.add(user));
	}

	@DeleteMapping("/user/{username}")
	public @ResponseBody RespEntity delete(@PathVariable("username") String username){
		return RespEntity.ok(userService.delete(username));
	}

	@PutMapping("/user")
	public @ResponseBody RespEntity update(PUserDto user){
		return RespEntity.ok(userService.update(user));
	}

	@GetMapping("/user")
	public @ResponseBody RespEntity queryUser(PUserDto dto){
		return RespEntity.ok(userService.page(dto));
	}

	@RequestMapping(params="service=user.updatepwd")
	public @ResponseBody RespEntity updatePwd(Principal principal,String old_pwd, String new_pwd){
		return RespEntity.ok(userService.updatePwd(old_pwd,new_pwd,principal.getName()));
	}
	
	//设置角色
	@GetMapping("/user/{username}/role")
	public @ResponseBody RespEntity queryUserRole(@PathVariable("username") String username){
		Map<String,Object> result = new HashMap();
		List<SecurityGroup> roles = roleService.list();
		result.put("roles",roles);
		result.put("selected", userService.queryUserRole(username));
		return RespEntity.ok(result);
	}
	@PutMapping("/user/{username}/role")
	public @ResponseBody RespEntity addUserRole(
			@RequestParam(value="roleIds[]",required=false)List<Integer> roleIds,
			@PathVariable("username") String username){
		return RespEntity.ok(userService.addUserRole(username,roleIds));
	}

	@PutMapping("/user/{username}/right")
	public @ResponseBody RespEntity addUserRight(
			@RequestParam(value="rightIds[]",required=false)List<String> rightIds,
			@PathVariable("username") String username){
		return RespEntity.ok(userService.addUserRight(username,rightIds));
	}

	@GetMapping({"/user/{username}/modal_authority.html", "/role/{id}/modal_authority.html"})
	public String modalAuthority(Model model,
			@PathVariable(value = "username",required = false) String username,
			@PathVariable(value = "id",required = false)Integer roleId){
		model.addAttribute("groupAuthorities",adminSecurityPropertity.getGroupAuthority());
		model.addAttribute("selected",StringUtils.isEmpty(username) ?
						roleService.queryRoleRight(roleId): userService.queryUserAuthorities(username));
		return "permission/modal_authority";
	}

}
